﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Thrift.Protocols;
using Thrift.Transports;
using Thrift.Transports.Client;
using RpcService.Api1Service;
using RpcClient;
using M.Core.Utils;
using Newtonsoft.Json;
using M.Core.RmoteCall.RpcCall;

namespace M.ServiceApi2.Controllers
{
    //[Route("api/[controller]")]
    [ApiController]
    public class Api2Controller : ControllerBase
    {
        //TClientTransport trans;
        //public Api2Controller()
        //{
        //    trans = new TSocketClientTransport(IPAddress.Parse("192.168.1.102"), 8800);
        //    trans.OpenAsync().Wait();
        //}

        [HttpGet]
        [Route("api/api2/GetUserByIDAsync")]
        public ActionResult<object> GetUserByIDAsync([FromServices] UserServiceRpc userServiceRpc)
        {
            try
            {
                var task = userServiceRpc.GetUserByIDAsync(0, CancellationToken.None);
                var a = task.GetAwaiter().GetResult();
                if (a == null) throw new Exception();

                return a;
            }
            catch (Exception)
            {

                System.Console.WriteLine("一只一只一只一只一只一只一只一只一只一只一只一只一只一只一只一只一只一只一只一只一只一只一只一只一只一只");
            }
            return null;
        }

        //[HttpGet]
        //[Route("api/api2/GetUserByIDAsyncV1")]
        //public ActionResult<object> GetUserByIDAsyncV1([FromServices] OrderServiceRpc userServiceRpc)
        //{
        //    if (!trans.IsOpen)
        //    {
        //        trans.OpenAsync().Wait();
        //    }
        //    IUserService.Client client = new IUserService.Client(new TMultiplexedProtocol(new TBinaryProtocol(new TFramedClientTransport(trans)), "UserService"));

        //    return client.GetUserByIDAsync(0,CancellationToken.None).GetAwaiter().GetResult();
        //}


        [HttpGet]
        [Route("api/api2/GetOrderByIDAsync")]
        public ActionResult<object> GetOrderByIDAsync([FromServices] OrderServiceRpc userServiceRpc)
        {
            var task = userServiceRpc.GetUserByIDAsync(0, CancellationToken.None);
            if (task == null) return "";

            return task.GetAwaiter().GetResult();
        }

        [HttpGet]
        [Route("api/api2/api1/getV1")]
        public ActionResult<object> GetApi1GetV1([FromServices] InnerApi api1Interface)
        {
            var url = $"http://192.168.1.102:1060/api/Api1";


            var json = HttpUtil.HttpWebRestfulGet(url, null);

            return JsonConvert.DeserializeObject(json,typeof(string[]) );
        }
        [HttpGet]
        [Route("api/api2/api1/get")]
        public ActionResult<object> GetApi1Get([FromServices] InnerApi api1Interface)
        {
            return api1Interface.Get();
        }


        [HttpGet]
        [Route("api/api2/getByID")]
        public ActionResult<object> GetApi1GetByID([FromServices] InnerApi api1Interface)
        {
            return api1Interface.Get(1);
        }

    }
}
